﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServerIntegration.SystemTransfers;

namespace ServerIntegration
{
    public class TransferCharacterNWoDVampire : TransferCharacter
    {
        public TransferCharacterNWoDVampire(TransferDataNWoDVampire data)
        {
            _name = data.CharacterName;
            _characterImageLocation = data.DownloadImageFromURLToLocal();
            GameID = data.GameID;
            ID = data.CharacterID;
            StringTraits = data.GetAllStringTraits();
            NumberTraits = data.GetAllIntTraits();
        }

        private readonly string _name;

        public override string Name
        {
            get { return _name; }
        }

        private readonly String _characterImageLocation;

        public override string CharacterImageLocation
        {
            get { return _characterImageLocation; }
        }

        public override string SystemLabel
        {
            get { return NWoDSystemLabel; }
        }

        private List<TransferTrait<String>> _stringTraits;

        public List<TransferTrait<String>> StringTraits
        {
            get { return _stringTraits; }
            set { _stringTraits = value; }
        }

        public override string SheetDescription
        {
            get
            {
                var sb = new StringBuilder();
                foreach (var cur in StringTraits)
                {
                    sb.AppendLine(cur.ToString());
                }
                foreach (var cur in NumberTraits)
                {
                    sb.AppendLine(cur.ToString());
                }
                return sb.ToString();
            }
        }

        private List<TransferTrait<int>> _numberTraits;

        public List<TransferTrait<int>> NumberTraits
        {
            get { return _numberTraits; }
            set { _numberTraits = value; }
        }

        private int ReturnIntFromNumericTraits(string label)
        {
            var firstOrDefault = NumberTraits.FirstOrDefault(x => x.Label.ToLower().Equals(label.ToLower()));
            if (firstOrDefault != null)
                return firstOrDefault.Contents;
            return 0;
        }

        private Stat ReturnNumericStat(string label)
        {
            if (HasNumericTrait(label))
            {
                return new Stat(ReturnIntFromNumericTraits(label));
            }
            return null;
        }

        private bool HasNumericTrait(string label)
        {
            return NumberTraits.Any(x => x.Label.ToLower().Equals(label.ToLower()));
        }

        public Stat Strength
        {
            get
            {
                return ReturnNumericStat(
                    "Strength"
                    );
            }
        }

        public Stat Dexterity
        {
            get
            {
                return ReturnNumericStat(
                    "Dexterity"
                    );
            }
        }

        public Stat Firearms
        {
            get
            {
                return ReturnNumericStat(
                    "Firearms"
                    );
            }
        }

        public Stat Weaponry
        {
            get
            {
                return ReturnNumericStat(
                    "Weaponry"
                    );
            }
        }

        public Stat Brawl
        {
            get
            {
                return ReturnNumericStat(
                    "Brawl"
                    );
            }
        }

        public Stat Stamina
        {
            get
            {
                return ReturnNumericStat(
                    "Stamina"
                    );
            }
        }

        public Stat Health
        {
            get
            {
                var stat1 = ReturnNumericStat(
                    "Size"
                    );
                var stat2 = ReturnNumericStat(
                    "Stamina"
                    );
                return new Stat(stat1.Value + stat2.Value);
            }
        }

        public Stat MeleeDefense
        {
            get
            {
                var stat1 = ReturnNumericStat("Wits");
                if (stat1.Value < Dexterity.Value)
                    return stat1;
                return Dexterity;
            }
        }

        public Stat Speed
        {
            get
            {
                var stat1 = ReturnNumericStat("Size");
                return new Stat(Strength.Value + Dexterity.Value + stat1.Value);
            }
        }

        public Stat Height
        {
            get { return new Stat(5); }
        }

        public Stat RangedDefense
        {
            get { return new Stat(0); }
        }
    }
}